package com.cxg.fairy.ui.recycler;

import android.content.Context;
import android.database.DataSetObserver;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.cxg.fairy.ui.R;
import java.util.ArrayList;

/**
 * Created by Administrator on 2017/12/25.
 */

public class MultiSnapHorizontalAdapter extends RecyclerView.Adapter<MultiSnapHorizontalAdapter.ViewHolder> implements Adapter {

    private ArrayList<JSONObject> mSnapItemList;
    //设置图片加载策略
    private static final RequestOptions RECYCLER_OPTIONS = new RequestOptions().centerCrop().diskCacheStrategy(DiskCacheStrategy.ALL).dontAnimate();

    protected Context mContext;

    public MultiSnapHorizontalAdapter(ArrayList<JSONObject> mSnapItemList) {
        this.mSnapItemList = mSnapItemList;
    }

    @Override
    public MultiSnapHorizontalAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getRootView().getContext()).inflate(R.layout.item_multi_snap_horizontal, viewGroup, false);
        return new MultiSnapHorizontalAdapter.ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MultiSnapHorizontalAdapter.ViewHolder holder, int position) {
//        String title = titles[position];
        String item_name = mSnapItemList.get(position).getString("item_name");
        String item_price = mSnapItemList.get(position).getString("item_price");
        String item_units = mSnapItemList.get(position).getString("item_units");
        String item_image = mSnapItemList.get(position).getString("item_image");
        holder.item_name.setText(item_name);
        holder.item_price.setText(item_price);
        holder.item_units.setText(item_units);
        ImageView mImageView = (ImageView) holder.itemView.findViewById(R.id.snap_item_image);
        //TODO:itemView 回退丢失
        Glide.with(holder.itemView.getContext()).load(item_image).apply(RECYCLER_OPTIONS).thumbnail(0.1f).into(mImageView);
    }

    @Override
    public int getItemCount() {
        if (mSnapItemList != null) {
            return mSnapItemList.size();
        } else {
            return 0;
        }
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public int getCount() {
        return 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return convertView;
    }

    @Override
    public int getViewTypeCount() {
        return 0;
    }

    @Override
    public boolean isEmpty() {
        return false;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private TextView item_name;
        private TextView item_price;
        private TextView item_units;
        private ImageView item_image;

        ViewHolder(final View itemView) {
            super(itemView);
            this.item_name = (TextView) itemView.findViewById(R.id.snap_item_title);
            this.item_price = (TextView) itemView.findViewById(R.id.snap_item_price);
            this.item_units = (TextView) itemView.findViewById(R.id.snap_item_units);
            this.item_image = (ImageView) itemView.findViewById(R.id.snap_item_image);
        }
    }
}
